<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\User;

class UserController extends Controller
{

    // 用户登录

     // 用户登录
     public function login(Request $request){

        //return $request;

        $bool = \Auth("web")->attempt($request->only("email","password"));
        //$bool = true;

        if($bool){

            //1 更新数据库表中的token

            $api_token = session_create_id();
            $user = User::where("email",$request->email)->first();
            $user-> api_token = $api_token;
            $res = $user->save();

            return response()->json(["status"=>200,"msg"=>"登录成功!","data"=>$user]);

        }else{
            return response()->json(["status"=>3000,"msg"=>"用户名或密码有误"]);
        }
        
        
     }
    // 用户注册 
    public function register(Request $request){

        $arr =  $request->toArray();
        $arr['api_token'] =  session_create_id();
        $arr['password'] = bcrypt($arr['password']);
        User::create($arr);
        unset($arr['password']);
        return response()->json(['status'=>200,'msg'=>'注册成功!','data'=>$arr]);
        
    }

   
     // 生成令牌
     private function getToken(){
        $random = $this->getRandChar(32);
        $time =  time();
        $salt = config("app.token_salt");
        $token_str = $random.$time.$salt;
        $token = md5($token_str);
        return $token;
    }

    // 生成随机字符串
    private function getRandChar($length)
    {
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol) - 1;

        for ($i = 0; $i < $length; $i++) {
            $str .= $strPol[rand(0, $max)];
        }
        return $str;
    }

    // 重置密码
    public function resetpass(){
        return view("user/resetpass");
    }

    // 重置密码
    public function resetp(Request $request){
        return $request->toArray();
    }


    //上传头像
    public function seticon(){
        $user=Auth::user();
        return view("user/seticon",compact("user"));
    }

    // 保存头像
    public function saveicon(Request $request){


        // 上传
        if ($request->hasFile('pic')) {
            $file = $request->file('pic');  //获取UploadFile实例
            if ( $file->isValid()) { //判断文件是否有效
                //$filename = $file->getClientOriginalName(); //文件原名称
                $ext = $file->getClientOriginalExtension(); //扩展名
                $filename = time() . "." . $ext;    //重命名
                $path=base_path("public\upload");
                $res=$file->move($path, $filename); //移动至指定目录
            }
        }


        // 入库
        if($res){

            // 删除原图
            $user = Auth::user();

            if($user->pic){
                $oldpic=$path."\\".$user->pic;
                if(file_exists($oldpic)){
                    unlink($oldpic);
                }
            }


            // 修改数据库
            $user=User::find(Auth::id());
            
            $user->pic = $filename;

            $user->save();


        }

        return back();
        
    }



}
